package com.zl.tby.screen.service.impl;

import com.zl.tby.screen.service.ITbyCommunityScreenService;
import com.zl.tby.screen.vo.community.TbyCommunityBean;
import com.zl.tby.screen.vo.mountains.TbyMountainsScreenBean;
import org.noear.solon.annotation.Component;
import org.noear.wood.DbContext;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 社区共管大屏service
 * @author changpeng
 * @version 1.0
 * @since 2024/8/22 15:40
 */
@Component
public class TbyCommunityScreenServiceImpl implements ITbyCommunityScreenService {

    @Db
    private DbContext db;

    @Override
    public TbyCommunityBean statistic() {
        TbyCommunityBean entity = TbyCommunityBean.statisticInit();
        try {
            List<TbyCommunityBean> villageList = db.table("tby_village_info").groupBy("village_type")
                    .selectList("count(id) value, village_type type", TbyCommunityBean.class);
            if (villageList != null) {
                for (TbyCommunityBean bean : villageList) {
                    if (bean.getType().intValue() == 0) {
                        entity.setRegionVillageCount(bean.getValue());
                    } else if (bean.getType().intValue() == 1) {
                        entity.setVillageCount(bean.getValue());
                    }
                }
            }
            List<TbyCommunityBean> sumList = db.table("tby_village_info").whereEq("village_type", 0)
                    .selectList("sum(registered_population) registered_population, sum(permanent_population) permanent_population, sum(labour_force) labour_force", TbyCommunityBean.class);
            if (sumList != null && sumList.size() > 0) {
                TbyCommunityBean row = sumList.get(0);
                entity.setLabourForce(row.getLabourForce() == null ? 0 : row.getLabourForce());
                entity.setPermanentPopulation(row.getPermanentPopulation() == null ? 0 : row.getPermanentPopulation());
                entity.setRegisteredPopulation(row.getRegisteredPopulation() == null ? 0 : row.getRegisteredPopulation());
            }
        } catch (Exception ex) {
            return TbyCommunityBean.statisticInit();
        }
        return entity;
    }

    @Override
    public List<TbyCommunityBean> workDynamicsStatistic(String createTimeBegin, String createTimeEnd) {
        try {
            List<TbyCommunityBean> dbList  = db.table("tby_work_dynamics").whereTrue()
                    .andIf(StringUtils.isNotEmpty(createTimeBegin), " create_time >= ? ", createTimeBegin + " 00:00:00")
                    .andIf(StringUtils.isNotEmpty(createTimeEnd), " create_time <= ? ", createTimeEnd + " 23:59:59")
                    .groupBy("type").selectList("count(id) value, type", TbyCommunityBean.class);
            long total = 0;
            for (TbyCommunityBean entity : dbList) {
                if (entity.getValue() != null) {
                    total = entity.getValue().longValue() + total;
                }
            }
            for (TbyCommunityBean item : dbList) {
                item.setTotal(total);
                item.cal();
            }
            Collections.sort(dbList, Comparator.comparingDouble(TbyCommunityBean::getPercentage).reversed());
            TbyCommunityBean.resetPercentage(dbList);
            return dbList;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ArrayList<>();
    }
}
